package com.hq.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    private static final String code="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
    private static final String number="1234567890";

//    判断源字符串是否有值，空引号也算没值
    public static boolean isEmpty(String str){
        if(str==null|| str==""){
            return  false;
        }else {
            return true;
        }
    }
    //判断源字符串是否有值，空引号和空格也算没值
    public static boolean isEmptyAndSpace(String str){
        String trim = str.trim();
        return isEmpty(trim);
//        if(str==null|| str==""){
//            return  false;
//        }else {
//            return true;
//        }
    }

    public static boolean isPhone(String phone){
        if(isEmpty(phone)){
            Pattern p = Pattern.compile("^1(3\\d{2}|4[14-9]\\d|5([0-35689]\\d|7[1-79])|66\\d|7[2-35-8]\\d|8\\d{2}|9[13589]\\d)\\d{7}$");
            Matcher m = p.matcher(phone);
            boolean matches = m.matches();
            return matches;
        }else
            return false;
    }


    public static boolean isEmail(String email){
        if(isEmpty(email)){
            Pattern p = Pattern.compile("^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
            Matcher m = p.matcher(email);
            boolean matches = m.matches();
            return matches;
        }else
            return false;
    }

    public static boolean isABC(String abc){
        if(isEmpty(abc)){
            Pattern p = Pattern.compile("^[A-Za-z]+$");
            Matcher m = p.matcher(abc);
            boolean matches = m.matches();
            return matches;
        }else
            return false;
    }

    public static String randomCode(int num){

        // 创建一个字符串缓冲区用来存储中间值
        StringBuffer sb=new StringBuffer();
        // 将常量值  拆成数组去随机字符
        String[] codes = code.split("");
//        char[] chars = code.toCharArray();
        //0~codes.length
        for (int i=0;i<num;i++){
           int n= (int)(Math.random()*codes.length);
           sb.append(codes[n]);
        }
        return  sb.toString();
    }



    public static String randomCodeAndNum(int num){

        // 创建一个字符串缓冲区用来存储中间值
        StringBuffer sb=new StringBuffer();
        // 将常量值  拆成数组去随机字符
        String[] codes = (code+number).split("");
//        char[] chars = code.toCharArray();
        //0~codes.length
        for (int i=0;i<num;i++){
            int n= (int)(Math.random()*codes.length);
            sb.append(codes[n]);
        }
        return  sb.toString();
    }


    public static String randomCC(int num){
        StringBuffer sb=new StringBuffer();
        int max = Integer.parseInt("9fa5", 16);
        int min = Integer.parseInt("4e00", 16);
        for (int i = 0; i < num; i++) {
            int rm= (int)(Math.random()*(max-min+1)+min);
            char c=(char)rm;
            sb.append(c);
        }
        return sb.toString();
    }
}
